package com.my.serviceimpl;

import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.my.bean.*;
import com.my.mapper.RoomMapper;
import com.my.service.*;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 房间 服务实现类
 * </p>
 *
 * @author 张子艺
 * @since 2018-08-30
 */
@Service
public class RoomServiceImpl extends ServiceImpl<RoomMapper, Room> implements RoomService {
    @Autowired
    ProductionService productionService;
    @Autowired
    RelationProjectService relationProjectService;
    @Autowired
    OrderVipService orderVipService;
    @Autowired
    MotherNursingService motherNursingService;
    @Autowired
    BabyNursingService babyNursingService;
    @Autowired
    FoodRelationService foodRelationService;
    @Override
    public List<Room> findProjectByHomeName(String[] data1,String[] data2,String param,Long userId,boolean admin,long areaid) {
        //定义 sql属性解析器
  /*      EntityWrapper ew=new EntityWrapper();
        if (name!=null&&name!=""){
            //向解析器中反射实体
            ew.setEntity(new Room());
            //注入条件              模糊查询的方式枚举类  default是两边%
            ew.like("room_name",name, SqlLike.DEFAULT);
        }
*/
        return baseMapper.selectHomeList(data1,data2,param,userId,admin,areaid);
    }

    @Override
    public List<Room> selectHomeListBySerch(String param, Long userId, boolean admin,long areaid) {
        return baseMapper.selectHomeListBySerch(param,userId,admin,areaid);
    }

    @Override
    public Long findOrderIdByHomeId(Long homeId) {
        return baseMapper.findOrderIdByHomeId(homeId);
    }

    @Override
    public List<Room> findAllMealNameByHomeId(Long homeId) {
        return baseMapper.findAllMealNameByHomeId(homeId);
    }

    //查询出该房间未退房的所有消费
    @Override
    public Map<String, String> findAllMoneyByHomeId(Long homeId) {
        Map map=baseMapper.findAllMoneyByHomeId(homeId);
        if(map==null){
            return baseMapper.findAllOrderMoneyByHomeId(homeId);
        }else{
            return  map;
        }

    }

    @Override
    @Transactional(rollbackFor = {SQLException.class})
    public String overHome(Long homeId) throws SQLException {
        boolean flag=false;
        //查询出该房间关联的所有的产康项目关联ID
        List<Long> allProjectId=baseMapper.findAllProjectByHomeId(homeId);
        //判断 如果 该房间有该额外项目  就去解除关联
        if(allProjectId.size()>0){
            List<RelationProject> relationProjects=new ArrayList<>();
            //循环添加需要修改的产康项目ID
            allProjectId.stream().forEach(s->{
                RelationProject relationProject=new RelationProject();
                relationProject.setId(s);
                relationProject.setIsState(1);
                relationProjects.add(relationProject);
            });
            //修改产康项目的退房状态为已退房
            flag=relationProjectService.updateBatchById(relationProjects);
        }else{
            flag=true;
        }

        if (!flag){
            throw  new SQLException();
            //修改产康项目成功后
        }else{
            //修改 订单房间已退房
            Map map=new HashMap<>();
            map.put("home_id",homeId);
            List<OrderVip > orderVips=orderVipService.selectByMap(map);
            //判断 如果 该房间有该额外项目  就去解除关联
            if(orderVips.size()>0){
                orderVips.stream().forEach(s->{
                    s.setWhetherHome(1);
                });
                flag=orderVipService.updateBatchById(orderVips);
            }else{
                flag=true;
            }

            if(!flag){
                throw  new SQLException();
            }else{
                //修改妈妈护理记录已退房
                Map map2=new HashMap<>();
                map2.put("room_id",homeId);
                List<MotherNursing> motherNursings= motherNursingService.selectByMap(map2);
                //判断 如果 该房间有该额外项目  就去解除关联
                if(motherNursings.size()>0){
                    motherNursings.stream().forEach(s->{
                        s.setIsDelete(1+"");
                    });
                    flag= motherNursingService.updateBatchById(motherNursings);
                }else{
                    flag=true;
                }

                if(!flag){
                    throw  new SQLException();
                }else{
                    //修改宝宝护理记录已退房
                    Map map3=new HashMap<>();
                    map3.put("room_id",homeId);
                    List<BabyNursing> babyNursings= babyNursingService.selectByMap(map3);
                    //判断 如果 该房间有该额外项目  就去解除关联
                    if(babyNursings.size()>0){
                        babyNursings.stream().forEach(s->{
                            s.setIsDelete(1);
                        });
                        flag= babyNursingService.updateBatchById(babyNursings);
                    }else{
                        flag=true;
                    }

                    if(!flag){
                        throw  new SQLException();
                    }else{
                        //修改房间菜单食物
                        Map map4=new HashMap<>();
                        map4.put("room_id",homeId);
                        List<FoodRelation> foodRelations= foodRelationService.selectByMap(map4);
                        if(foodRelations.size()>0){
                            foodRelations.stream().forEach(s->{
                                s.setIsDelete(1);
                            });
                            flag= foodRelationService.updateBatchById(foodRelations);
                        }else{
                            flag=true;
                        }

                        if(!flag){
                            throw  new SQLException();
                        }else{
                            //修改房间状态
                            Room room=new Room();
                            room.setId(homeId);
                            room.setRoomStatus(1031511187990364162L);
                            if(this.updateById(room)){

                                return "退房成功";
                            }else{
                                throw  new SQLException();
                            }

                        }
                    }
                }
            }
        }


    }

    @Override
    public List<Room> findRoomByNoOrder(Long userId,boolean admin,long areaid) {
        return baseMapper.findRoomByNoOrder(userId, admin , areaid);
    }
}
